package com.qf.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsGoodsMapper;
import com.qf.pojo.DtsGoods;
import com.qf.service.DtsGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author ZhangYuJie
 * projectName:shop_manage
 * time:2022/5/17 8:21
 * description:
 */
@Service
public class DtsGoodsServiceImpl implements DtsGoodsService {

    @Autowired
    DtsGoodsMapper dtsGoodsMapper;

    @Override
    public int count() {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("deleted",false);
        Integer count=dtsGoodsMapper.selectCount(wrapper);
        return count;
    }

    @Override
    public IPage<DtsGoods> querySelective(String goodsSn, String name, Integer page, Integer size, String sort, String order, List<Integer> brandIds) {
        IPage<DtsGoods> ipage = new Page<>(page, size);
        QueryWrapper<DtsGoods> wapper = new QueryWrapper<>();

        wapper.eq("deleted", false);

        if (!StringUtils.isEmpty(goodsSn)) {
            wapper.eq("goods_sn", goodsSn);
        }
        if (!StringUtils.isEmpty(name)) {
            wapper.like("name", name);
        }
        if (brandIds != null && brandIds.size() > 0) {
            wapper.in("brand_id", brandIds);
        }

        //排序
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("asc")) {
                wapper.orderByAsc(sort);
            } else {
                wapper.orderByDesc(sort);
            }

        }

        ipage = dtsGoodsMapper.selectPage(ipage, wapper);
        return ipage;
    }

    @Override
    public int updateById(DtsGoods goods) {
        goods.setUpdateTime(new Date());
        return dtsGoodsMapper.updateById(goods);
    }

    @Override
    public void deleteById(Integer id) {
        dtsGoodsMapper.deleteById(id);
    }

    @Override
    public DtsGoods findById(Integer id) {
        QueryWrapper<DtsGoods> wapper = new QueryWrapper<>();
        wapper.eq("id", id);
        wapper.eq("deleted", false);
        DtsGoods one = dtsGoodsMapper.selectOne(wapper);
        return one;
    }

    @Override
    public boolean checkExistByName(String name) {
        QueryWrapper<DtsGoods> wapper = new QueryWrapper<>();
        wapper.eq("name", name);
        wapper.eq("sales", true);
        wapper.eq("deleted", false);
        Integer count = dtsGoodsMapper.selectCount(wapper);
        return count != 0;
    }

    @Override
    public void add(DtsGoods goods) {
        goods.setAddTime(new Date());
        goods.setUpdateTime(new Date());
        dtsGoodsMapper.insert(goods);
    }
}
